<html>
<head>
<title>Demo of VLC mozilla plugin</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$("#submit").click(function() {
			var paragraph = $("#paragraph").val();
			var vlc = document.getElementById('vlc');
            var text = splitParagraph(paragraph);
            var number = 0;
            
            var first = vlc.playlist.add("http://translate.google.com.vn/translate_tts?tl=en&q="+text[0]);
            for(var i = 1; i < text.length; i++){
                vlc.playlist.add("http://translate.google.com.vn/translate_tts?tl=en&q="+text[i]);
            }
            
            vlc.playlist.playItem(first);
            number++;
            var idInterval = setInterval(function(){
                if(number == text.length){
                    vlc.playlist.clear();
                    clearInterval(idInterval);
                }else{
                    vlc.playlist.next();
                    number++;
                }
            }, 2000);
            
            function splitParagraph(text){
                var textArr = text.split(".");
                return textArr;
            }
            
            
            
            
            function registerVLCEvent(event, handler)
            {
                var vlc = document.getElementById('vlc');
                if (vlc) {
                    if (vlc.attachEvent) {
                        // Microsoft
                        vlc.attachEvent (event, handler);
                    } else if (vlc.addEventListener) {
                        // Mozilla: DOM level 2
                        vlc.addEventListener (event, handler, false);
                    } else {
                        // DOM level 0
                        vlc["on" + event] = handler;
                    }
                }
            }
            
            // stop listening to event
            function unregisterVLCEvent(event, handler)
            {
                var vlc = document.getElementById('vlc');
                if (vlc) {
                    if (vlc.detachEvent) {
                        // Microsoft
                        vlc.detachEvent (event, handler);
                    } else if (vlc.removeEventListener) {
                        // Mozilla: DOM level 2
                        vlc.removeEventListener (event, handler, false);
                    } else {
                        // DOM level 0
                        vlc["on" + event] = null;
                    }
                }
            }
            
            // event callback function for testing
            function handlePauseEvent(event)
            {
                if (!event)
                    event = window.event; // IE
                if (event.target) {
                    // Netscape based browser
                    targ = event.target;
                } else if (event.srcElement) {
                    // ActiveX
                    targ = event.srcElement;
                } else {
                    // No event object, just the value
                    if(event == 1){
                        
                        //unregisterVLCEvent('MediaPlayerPositionChanged', this)
                    }
                    return;
                }
                if (targ.nodeType == 3) // defeat Safari bug
                    targ = targ.parentNode;
                alert("Event " + event.type + " has fired from " + targ );
            }
            
            // Register a bunch of callbacks.
            registerVLCEvent('MediaPlayerPositionChanged', handlePauseEvent);
    		});
	});
</script>
</head>

<body>

<h2>Input text</h2>
<textarea id="paragraph" cols="30" rows="5" ></textarea>
<button id="submit">Listen</button>
<embed id="vlc" type="application/x-vlc-plugin" width="400" height="300" />


</body>
</html>
